/**
 * 
 */
package com.bitBusy.manageyourriches.database.queries;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.bitBusy.manageyourriches.database.tables.PreviouslyChosenValues;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;

/**
 * All queries for previously chosen values table
 * @author SumaHarsha
 *
 */
public class QueriesPreviouslyChosenValuesTable extends DatabaseQueries{

	/**Default payment info method to remember for the category*/
	private static final String DEFAULTPAYMENTINFO = "cash";
	/**
	 * Constructor - calls superclass constructor
	 * @param the_context
	 */
	public QueriesPreviouslyChosenValuesTable(Context the_context) {
		super(the_context);
	}
	
	/** Name of table related to queries in this class*/
	private static final String tableName = PreviouslyChosenValues.class.getSimpleName();
	
	/** String for the category column name*/
	private static final String categoryCol = PreviouslyChosenValues.CATEGORYCOL.getName();
			
	/** String for the subcategory column name*/
	private static final String subcategoryCol = PreviouslyChosenValues.SUBCATEGORYCOL.getName();
	
	/** String for the paymentinfo column name*/
	private static final String paymentMethodCol = PreviouslyChosenValues.PAYMENTINFOCOL.getName();
	
			
	/** String array of all column names in this table*/
	//private static final String[] allColumns = new String[]{categoryCol, subcategoryCol, paymentMethodCol};
	
	/** Reference to the Cursor object used to store and process query results*/
	private Cursor cursor;



	/** Method to add categories
	 * @param the database object
	 * @param list of category names
	 */
	public void addCategories(List<String> the_categoryNames,
			SQLiteDatabase the_db)
	{
		if (the_categoryNames != null && the_db != null)
		{
			myDatabase = the_db;
			try
			{
		
				ContentValues content = new ContentValues();
				content.put(paymentMethodCol, DEFAULTPAYMENTINFO);
				for (String category : the_categoryNames)
				{
					content.put(categoryCol, category);
					myDatabase.insertOrThrow(tableName, null, content);
		
				}
			}
			catch(SQLiteException exception)
			{
				DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.DIALOGNONUNIQUECATNAME.getPrompt());
			}
	}
		else
		{
			DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());

		}
		
	}
	
	
	/** Method to remember choice for subcategory
	 * @param category name
	 * @param subcategory name
	 */
	public void rememberSubcategory(String the_categoryName, String the_subcategoryName)
	{
		boolean iOpened = checkAndOpen();
		if (myDatabase != null && the_categoryName != null && the_subcategoryName != null)
		{
			
			ContentValues content = new ContentValues();
			if (the_subcategoryName != null)
			{
				content.put(subcategoryCol, the_subcategoryName);
			}
			
			myDatabase.update(tableName, content, categoryCol + "=?", new String[] {the_categoryName});
			checkAndClose(iOpened);
			
		}	
		
		else
		{
			DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());
		}
	}
	
	/** Method to remember choice for payment method
	 * @param category name
	 * @param payment method name
	 */
	public void rememberPaymentMethod(String the_categoryName, String the_paymentMethod)
	{
		boolean iOpened = checkAndOpen();
		if (myDatabase != null && the_categoryName != null && the_paymentMethod != null)
		{
			
			ContentValues content = new ContentValues();
			if (the_paymentMethod != null)
			{
				content.put(paymentMethodCol, the_paymentMethod);

			}
			
			myDatabase.update(tableName, content, categoryCol + "=?", new String[] {the_categoryName});
			checkAndClose(iOpened);
			
		}	
		
		else
		{
			DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());
		}
	}
	
	
	
	
	
	/** Method to get previous subcategory choice for the category
	 * @param category name
	 * @return subcategory name
	 */
	public String getChosenSubcategory(String the_categoryName)
	{
		String returnName = null;
		boolean iOpened = checkAndOpen();
		if (myDatabase != null && the_categoryName != null)
		{
			cursor = myDatabase.query(tableName, new String[]{subcategoryCol}, categoryCol + "=?", new String[]{the_categoryName}, null, null, null);
			if (cursor != null && cursor.moveToFirst())
			{
				returnName = cursor.getString((cursor.getColumnIndex(subcategoryCol)));
				cursor.close();
			}
		}
		checkAndClose(iOpened);
		return returnName;
	}
	
	/** Method to get previous payment method choice for the category
	 * @param category name
	 * @return payment method name
	 */
	public String getChosenPaymentMethod(String the_categoryName)
	{
		String returnName = null;
		boolean iOpened = checkAndOpen();
		if (myDatabase != null && the_categoryName != null)
		{
			cursor = myDatabase.query(tableName, new String[]{paymentMethodCol}, categoryCol + "=?", new String[]{the_categoryName}, null, null, null);
			if (cursor != null && cursor.moveToFirst())
			{
				returnName = cursor.getString((cursor.getColumnIndex(paymentMethodCol)));
				cursor.close();
			}
		}
		checkAndClose(iOpened);
		return returnName;
	}


	/**
	 * @param the_categoryChosen
	 */
	public boolean deleteCategory(String the_categoryChosen) {
		boolean success = false;
		if (the_categoryChosen != null)
		{
			boolean iOpened = checkAndOpen();
	    	if( myDatabase.delete(tableName, categoryCol + " = '" + the_categoryChosen +"'", null)  >= 0)
			{
				success = true;
			}
		checkAndClose(iOpened);
		}
		return success;
		
	}


	/**
	 * @param the_category
	 * @param the_subcategory
	 * @return
	 */
	public boolean deleteSubcategory(String the_category, String the_subcategory) {
		boolean success = false;
		if (the_subcategory != null)
		{
			boolean iOpened = checkAndOpen();
	    	if( myDatabase.delete(tableName, categoryCol + " = '" + the_category + "' AND " + 
	    						subcategoryCol + " = '" + the_subcategory +"'", null)  >= 0)
			{
				success = true;
			}
		checkAndClose(iOpened);
		}
		return success;
		}

	
}

